#include <iostream>
#include <vector>
#include <SDL/SDL.h>
#include "effects.h"
#include "gfx.h"
#include "highground.h"

extern unsigned int bpp;

Effects::Effects() {
	
}

Effects::~Effects() {
	tileTints_.clear();
}

void Effects::blit(SDL_Surface* surface) {
	for (int i = 0; i < (int)tileTints_.size(); i++) {
		tileTints_.at(i)->blit();
	}
}

void Effects::addTileTint(const int x, const int y,
                          const SDL_Color* const colour,
                          const unsigned char alpha) {
	TileTint* tileTint = new TileTint(x, y, colour, alpha);
	tileTints_.push_back(tileTint);
}

void Effects::removeTileTint(const int x, const int y) {
	for (int i = 0; i < (int)tileTints_.size(); i++) {
		if ((tileTints_.at(i)->getX() == x) && (tileTints_.at(i)->getY() == y)){
			delete tileTints_.at(i);
			tileTints_.erase(tileTints_.begin() + i);
		}
	}
}

void Effects::clearTileTints() {
	tileTints_.clear();
}

TileTint::TileTint(const int x, const int y, const SDL_Color* colour, 
		const unsigned char alpha) {
	x_ = x; y_ = y;
	colour_.r = colour->r;
	colour_.g = colour->g;
	colour_.b = colour->b;
	alpha_ = alpha;
}

TileTint::~TileTint() {

}

void TileTint::blit() {
	SDL_Surface* tintSurface =  SDL_CreateRGBSurface(SDL_SWSURFACE, 
			SPRITE_WIDTH, SPRITE_HEIGHT, bpp, 0, 0, 0, 0);
	fillRect(tintSurface, 0, 0, SPRITE_WIDTH, SPRITE_HEIGHT, SDL_MapRGB(
					tintSurface->format, colour_.r, colour_.g, colour_.b));
	drawToTile(tintSurface, x_, y_, alpha_);
	SDL_FreeSurface(tintSurface);
}

int TileTint::getX() const {
	return x_;
}

int TileTint::getY() const {
	return y_;
}

